#!/bin/sh
#
# retain kernel modules/firmware for use by the new rootfs (NFSROOT or local disk)

# abort if called by mkinitramfs directly
[ -z "$rootmnt" ] && exit

mount_overlayfs()
{
	# overlay over NFS/CIFS not supported yet
	# http://comments.gmane.org/gmane.linux.file-systems.union/190
	[ "$BOOT" = 'nfs' ] && return

	grep -q overlayfs /proc/filesystems || modprobe overlay || return

	local overlay_mnt=/overlay
	mkdir -p $overlay_mnt

	mount -t tmpfs none $overlay_mnt
	mkdir $overlay_mnt/upperdir
	mkdir $overlay_mnt/workdir

	mount -t overlay overlay $rootmnt -o rw,relatime,lowerdir=$rootmnt,upperdir=$overlay_mnt/upperdir,workdir=$overlay_mnt/workdir ||
	umount $overlay_mnt

	return

	cat >> /conf/param.conf <<EOF
export rootmnt=$overlay_mnt
EOF
export rootmnt=$overlay_mnt
}

copy_dir()
{
	local dir=$1

	mkdir -p		$rootmnt/$dir
	date >			$rootmnt/$dir/debug-premount
	mount -t tmpfs none	$rootmnt/$dir
	date >			$rootmnt/$dir/debug-postmount
	cp -a $dir/*		$rootmnt/$dir
}

mount_overlayfs

copy_dir /lib/modules
copy_dir /lib/firmware
copy_dir /lkp
# header, linux perf and kselftests
copy_dir /usr/src
